package view2;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.example.zhinongbao.R;
import com.example.zhinongbao.adapter.GoodsListAdapter;
import com.example.zhinongbao.base.BaseFragment;
import com.example.zhinongbao.contract.MainContract;
import com.example.zhinongbao.entity.Goods;
import com.example.zhinongbao.presenter.MainPresenter;
import com.example.zhinongbao.view.MainActivity2;

import java.util.ArrayList;
import java.util.List;

public class FragmentHome extends BaseFragment implements MainContract.IMainView {

    private RecyclerView mGoodsRecyclerView;
    private ImageView mSearchEdit,maijiatiao;

    private GoodsListAdapter mRecyclerAdapter;
    private MainContract.IMainPresenter mMainPresenter;
    private List<Goods> mGoodsList;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        mMainPresenter = new MainPresenter(this, getContext());
        mGoodsList = new ArrayList<>();
        mSearchEdit = mView.findViewById(R.id.sousuokuang);
        maijiatiao=mView.findViewById(R.id.tiaomaijia);
        mGoodsRecyclerView = mView.findViewById(R.id.rv_goods);
        StaggeredGridLayoutManager staggeredGridLayoutManager =
                new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        mGoodsRecyclerView.setLayoutManager(staggeredGridLayoutManager);
        mRecyclerAdapter = new GoodsListAdapter(mGoodsList);
        mGoodsRecyclerView.setAdapter(mRecyclerAdapter);
        // 为每个标签设置点击事件
        mView.findViewById(R.id.tab001).setOnClickListener(v -> switchTab(0));
        mView.findViewById(R.id.tab002).setOnClickListener(v -> switchTab(1));
        mView.findViewById(R.id.tab003).setOnClickListener(v -> switchTab(2));
        mView.findViewById(R.id.tab004).setOnClickListener(v -> switchTab(3));
        maijiatiao.setOnClickListener(v -> {
            // 创建意图跳转到MainActivity2
            Intent intent = new Intent(getActivity(), MainActivity2.class);
            startActivity(intent);
            getActivity().finish();
        });
    }
    private void switchTab(int category) {
        // 调用Presenter中的方法来获取特定类别的商品列表
        mMainPresenter.getGoodsByCategory(category);
        // 更新标签的视觉效果
        highlightTab(category);
    }

    private void highlightTab(int category) {
        // 重置所有标签的样式
        resetTabs();
        // 高亮显示当前选中的标签
        View tab = mView.findViewById(category == 0 ? R.id.tab001 :
                category == 1 ? R.id.tab002 :
                        category == 2 ? R.id.tab003 :
                                R.id.tab004);
        tab.setBackgroundResource(R.color.selected_tab_color); // 假设你有一个选中标签的颜色资源
    }

    private void resetTabs() {
        // 重置所有标签的背景
        mView.findViewById(R.id.tab001).setBackgroundResource(R.color.default_tab_color);
        mView.findViewById(R.id.tab002).setBackgroundResource(R.color.default_tab_color);
        mView.findViewById(R.id.tab003).setBackgroundResource(R.color.default_tab_color);
        mView.findViewById(R.id.tab004).setBackgroundResource(R.color.default_tab_color);
    }

    @Override
    public void onResume() {
        super.onResume();
        mMainPresenter.getGoodsList();
    }

    @Override
    protected void initListener() {

    }

    @Override
    public void showGoodsList(List<Goods> goodsList) {
        mGoodsList = goodsList;
        Log.d("", "showGoodsList " + mGoodsList.size());
        mRecyclerAdapter.updateData(mGoodsList);
    }

    @Override
    public List<Goods> getGoodsListByCategory(int category) {
        return mGoodsList;
    }



}